package com.wuage.infrastructure.utils;

import com.wuage.infrastructure.exception.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author shynin
 */
@ApiModel("返回结果")
public class Response<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final int NO_LOGIN = -1;

    public static final int SUCCESS = 0;

    public static final int FAIL = 1;

    @ApiModelProperty("信息")
    private String msg = "success";

    @ApiModelProperty("编码，0代表成功")
    private int code = SUCCESS;

    @ApiModelProperty("实体数据")
    private T data;

    public Response() {
        super();
    }

    public Response(T data) {
        super();
        this.data = data;
    }

    public Response(ResultCode resultCode) {
        super();
        this.msg = resultCode.getDesc();
        this.code = resultCode.getCode();
    }

    public Response(T data, ResultCode resultCode) {
        super();
        this.data = data;
        this.msg = resultCode.getDesc();
        this.code = resultCode.getCode();
    }

    public Response(T data, String msg) {
        super();
        this.data = data;
        this.msg = msg;
    }

    public Response(Throwable e) {
        super();
        this.msg = e.getMessage();
        this.code = FAIL;
    }

    public Response(int code, String message, T data) {
        super();
        this.msg = message;
        this.code = code;
        this.data = data;
    }

    public static <T> Response<T> success() {
        return new Response<>();
    }

    public static <T> Response<T> success(T data) {
        return new Response<>(data);
    }

    public static <T> Response<T> success(T data, String message) {
        return new Response<>(data, message);
    }

    public static <T> Response<T> error(ResultCode resultCode) {
        return new Response<>(resultCode);
    }

    public static <T> Response<T> error(T data, ResultCode resultCode) {
        return new Response<>(data, resultCode);
    }

    public static <T> Response<T> error(int code, String message, T data) {
        return new Response<>(code, message, data);
    }

    public static <T> Response<T> error(int code, String message) {
        return new Response<>(code, message, null);
    }

    public static <T> Response<T> error(String message) {
        return new Response<>(FAIL, message, null);
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
